/*
Copyright(c) 2008 Jari Kemppinen <jari@viddem.com>

This example was built upon the PureMVC Framework by Cliff Hall.
http://www.puremvc.org
This example uses the Java PureMVC Framework Version 0.2

Please contribute to the PureMVC community.

License
-------
Your reuse is governed by the Creative Commons Attribution 3.0 license.
 */

package org.lbroussal.client.view;

import org.lbroussal.client.ApplicationFacade;
import org.lbroussal.client.view.components.LoginScreen;
import org.puremvc4gwt.client.interfaces.INotification;
import org.puremvc4gwt.client.patterns.mediator.Mediator;
import org.puremvc4gwt.client.patterns.observer.Notification;

public class LoginScreenMediator extends Mediator {

    public static final String NAME = "LoginScreenMediator";

    private LoginScreen loginScreen = null;

    public LoginScreenMediator() {
        super(NAME, null, ApplicationFacade.LOGIN, ApplicationFacade.LOGIN_FAIL);
    }

    public void SUBMITLOGIN(String user, String pass) {
        String[] details = new String[] { user, pass };

        this.facade.notifyObservers(new Notification(ApplicationFacade.PROCESS_LOGIN, details, null));
        // It this indirection useful or we can call directly the controller?
        //new ProcessLogin().doLogin(user, pass);
    }

    public void EXIT() {
        ApplicationFacade.loginDemo.exitApp();
    }

    public LoginScreen getLoginScreen() {
        try {
            if (loginScreen == null) {
                loginScreen = new LoginScreen(this);
            }

            return loginScreen;
        } catch (Exception e) {
            System.out.println(e.toString());
            return null;
        }
    }

    public void handleNotification(INotification note) {
        if (note.getName().equals(ApplicationFacade.LOGIN)) {
            onLogin();
        } else if (note.getName().equals(ApplicationFacade.LOGIN_FAIL)) {
            onLoginFail();

        }
    }

    /**
     * @event handler
     */
    private void onLoginFail() {
        getLoginScreen().loginFail();
    }

    /**
     * @event handler
     */
    private void onLogin() {
        ApplicationFacade.loginDemo.setCurrentDisplay(getLoginScreen());
        getLoginScreen().showLoginBox();
    }
}